<?php
/**
 * sitedefine.php
 * Create on 2015-03-25 10:48:38
 * Create by yuebin
 */
namespace haibao\cms\business\site;
use haibao\frame\data\query\Condition;
use haibao\cms\business\BusinessException;
use haibao\cms\data\site\Site as SiteData;
use haibao\cms\model\filter\site\SiteDefine as SiteDefineFilter;

class Site extends \haibao\cms\business\Base{
	
	public static function getAllSiteDefine($filter){
		$data = new SiteData();
		
		return $data->getAll($filter);
	}
	
	/**
	 * 获取站点数
	 * @param : $siteId 站点ID
	 */
	public static function getSiteDefineTree($siteId){
		
		if(!$siteId){
			throw new BusinessException('站点ID不能为空');
		}

		$siteDefineData = new SiteData();
		return $siteDefineData->getSiteDefineTree($siteId);
	}
	/**
	 * 添加站点
	 * @param : Object \haibao\cms\model\data\site\SiteDefine $siteDefineModel
	 */
	public static function addSiteDefine($siteDefineModel){

		self::checkOperation(self::FUNC_CODE_SITE_ADD);
		
		if(!$siteDefineModel->SiteName){
			throw new BusinessException('站点名不能为空');
		}
		if(!$siteDefineModel->SiteUrl){
			throw new BusinessException('站点URL不能为空');
		}
		
		$siteDefineData = new SiteData();
		$siteDefineData->addSiteDefine($siteDefineModel);
		self::saveOperateHistory(self::FUNC_CODE_SITE_ADD, $siteDefineModel->toArray(true,true));
	}
	
	
	/**
	 * 更新站点
	 * @param : Object \haibao\cms\model\data\site\SiteDefine $siteDefineModel
	 */
	public static function updateSiteDefineById($siteDefineModel){
		
		self::checkOperation(self::FUNC_CODE_SITE_EDIT);
		
		if(!$siteDefineModel->SiteName){
			throw new BusinessException('站点名不能为空');
		}
		if(!$siteDefineModel->SiteUrl){
			throw new BusinessException('站点URL不能为空');
		}
		
		$siteDefineData = new SiteData();
		$siteDefineData->updateSiteDefineById($siteDefineModel);
		
		self::saveOperateHistory(self::FUNC_CODE_SITE_EDIT, $siteDefineModel->toArray(true,true));
		
	}
	
	/**
	 * 根据站点ID删除站点信息
	 * @param : Int $siteId
	 */
	public static function deleteSiteDefineById($siteId){

		self::checkOperation(self::FUNC_CODE_SITE_DELETE);
		if(!$siteId){
			throw new BusinessException('站点ID不能为空');
		}
		
		$siteDefineData = new SiteData();
		$siteDefineData->deleteSiteDefineById($siteId);
		self::saveOperateHistory(self::FUNC_CODE_SITE_DELETE, $siteId);
	}
	
	/**
	 * 获取站点列表
	 */
	public static function getSiteList(){
		
		$siteDefineFilter =  new SiteDefineFilter();
		$siteDefineFilter->select(array(
			SiteDefineFilter::CONDITION_FIELD_NAME_ID,
			SiteDefineFilter::CONDITION_FIELD_NAME_SITE_NAME,
		));
		$siteDefineFilter->where(SiteDefineFilter::CONDITION_FIELD_NAME_STATUS,Condition::CONDITION_NOTEQUAL,\haibao\cms\model\data\site\SiteDefine::STATUS_DIS_ENABLE);
		
		$data = new SiteData;
		return $data->getAll($siteDefineFilter);
	}
	
	/**
	 * 根据站点ID获取站点信息
	 * @param : Int $siteId
	 */
	public static function getSiteDefineById($siteId){
		
		$data = new SiteData;
		return $data->getSiteDefineById($siteId);
	}
	
	/**
	 * 判断当前站点下是否存在频道
	 * @pamam : Int $siteId 
	 */
	public static function isExistChannelForSite($siteId){

		$data = new SiteData;
		return $data->isExistChannel($siteId);
	}
	
	/**
	 * 根据站点名字获取站点信息
	 * @param : String $siteName
	 * @param : Int $siteId
	 */
	public static function getSiteBySiteName($siteName, $siteId = null){
		$data = new SiteData;
		return $data->getSiteBySiteName($siteName,$siteId);
		
	}
}
